package cn.wzvtc.soft14;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by linji on 2016/3/4.
 */
@RestController
@EnableAutoConfiguration
@ComponentScan
@CrossOrigin(origins = {},methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})
public class Test {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Test.class, args);
    }

    @Autowired
    KebiaoService kebiaoService;//new KebiaoService();

    /**
     * 根据周次和班级查找课表信息
     * 测试例子：
     * http://localhost:8080/getlessions?week=0&clazz=0
     * http://localhost:8080/getlessions?week=1&clazz=1
     * @param week -周次
     * @param clazz 课表
     * @return 反回一个符合条件的list，但是没关系，spring会把他们转成json数据返回给客户端的。
     */
    @RequestMapping("/getlessions")
    Object getkebiaobyweekclass(int week,int clazz) {
        return this.kebiaoService.getKebiao(week,clazz);
    }

    /**
     * 获取所有课程信息
     * @return
     */
    @RequestMapping("/getalllessions")
    Object getalllessions() {
        return this.kebiaoService.getAllLession();
    }



    @RequestMapping("/addoneless")
    //用前端增加课程
    void addoneless(String kcid,String name,String addr,String wzid,int kscx,String tea) {
        Ke p=new Ke();
        p.setId(kcid);
        p.setName(name);
        p.setAddr(addr);
        p.setWzid(wzid);
        p.setKccx(kscx);
        p.setTeacher(tea);
        p.setClazz(1);
        p.setWeek(1);
        this.kebiaoService.saveKe(p);

    }

    @RequestMapping("/deleteone")
    void deleteone() {
         this.kebiaoService.deleteoneLession("1001");
    }

    @RequestMapping("/deleteall")
    void deleteall() {
        this.kebiaoService.deleteallLession();
    }

    //@RequestMapping("/getlesson")

    @RequestMapping(value="/getlesson",produces = "application/json; charset=UTF-8")
    String getlesson(){
        return "[{\"name\":\"动态编程\"},{\"name\":\"数据分析\"},{\"name\":\"移动应用\"}]";
    }


    /**
     * 添加课程信息，
     * 暂时做到用浏览器访问http://lcalhost:8080/addlession 后，触发本方法，插入一条记录，
     * 但并未让用户在浏览器指定各参数。
     * @return
     */
    @RequestMapping("/addlession")
    void addlession() {
        Ke k=new Ke();
        //一定要指定id，否则数据库会报错。此处设置的uuid可以保证永远唯一
       // k.setId(UUID.randomUUID().toString());
        k.setId("1001");
        k.setName("数据库分析与设计");
        k.setClazz(1);
        k.setWzid("1-1");
        k.setKccx(2);
        k.setWeek(1);
        k.setTeacher("黄河");
        k.setAddr("好学楼512");
        this.kebiaoService.saveKe(k);


        Ke k1=new Ke();
        k1.setId("1002");
        k1.setName("数据库分析与设计");
        k1.setClazz(2);
        k1.setWzid("2-3");
        k1.setKccx(2);
        k1.setWeek(1);
        k1.setTeacher("黄河");
        k1.setAddr("好学楼511");
        this.kebiaoService.saveKe(k1);

        Ke k2=new Ke();
        k2.setId("1003");
        k2.setName("混合平台开发（特长班）");
        k2.setClazz(1);
        k2.setWzid("1-3");
        k2.setKccx(3);
        k2.setWeek(1);
        k2.setTeacher("尹特立");
        k2.setAddr("技术研发大楼");
        this.kebiaoService.saveKe(k2);


        Ke k3 =new Ke();
        k3.setId("1004");
        k3.setName("混合平台开发（特长班）");
        k3.setClazz(1);
        k3.setWzid("2-6");
        k3.setKccx(3);
        k3.setWeek(1);
        k3.setTeacher("尹特立");
        k3.setAddr("技术研发大楼");
        this.kebiaoService.saveKe(k3);

        Ke k4=new Ke();
        k4.setId("1005");
        k4.setName("混合平台开发（特长班）");
        k4.setClazz(1);
        k4.setWzid("4-3");
        k4.setKccx(3);
        k4.setWeek(1);
        k4.setTeacher("刘笑峰");
        k4.setAddr("技术研发大楼");
        this.kebiaoService.saveKe(k4);

        Ke k5=new Ke();
        k5.setId("1006");
        k5.setName("混合平台开发（特长班）");
        k5.setClazz(1);
        k5.setWzid("5-3");
        k5.setKccx(3);
        k5.setWeek(1);
        k5.setTeacher("刘笑峰");
        k5.setAddr("技术研发大楼");
        this.kebiaoService.saveKe(k5);

        Ke k6=new Ke();
        k6.setId("1007");
        k6.setName("思维与口才");
        k6.setClazz(1);
        k6.setWzid("4-1");
        k6.setKccx(2);
        k6.setWeek(1);
        k6.setTeacher("刘小方");
        k6.setAddr("好学楼505");
        this.kebiaoService.saveKe(k6);

        Ke k7=new Ke();
        k7.setId("1008");
        k7.setName("动态编程");
        k7.setClazz(1);
        k7.setWzid("5-1");
        k7.setKccx(2);
        k7.setWeek(1);
        k7.setTeacher("项道东");
        k7.setAddr("明德楼803");
        this.kebiaoService.saveKe(k7);
    }

}
